package com.ming_za.od_updater.core.window.page;

import com.ming_za.od_updater.core.updater.StateBridge;
import com.ming_za.od_updater.core.updater.UpdaterManager;
import com.ming_za.od_updater.core.window.LoadingState;
import com.ming_za.od_updater.core.window.PageManager;
import com.ming_za.od_updater.core.window.component.ProgressBar;
import javax.swing.*;
import java.awt.*;

/**
 * 更新页面
 */
public class UpdatePage implements IPage{

    @Override
    public void renderContent(JPanel panel) {

        panel.add(Box.createRigidArea(new Dimension(0, 5)));

        // 状态标签
        JLabel statusLabel = new JLabel(PageManager.updatingText, JLabel.CENTER);
        statusLabel.setFont(statusLabel.getFont().deriveFont(14f));
        statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(statusLabel);

        panel.add(Box.createRigidArea(new Dimension(0, 5)));

        // 进度条
        JLabel updateLabel = new JLabel("0/0 - 0kb/s", JLabel.CENTER);
        updateLabel.setFont(statusLabel.getFont().deriveFont(12f));
        updateLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        ProgressBar progressBar = new ProgressBar(unused -> {
            updateLabel.setText(StateBridge.getCompleted() + "/"+ StateBridge.getTotal() +" - "+StateBridge.getSpeed()+"kb/s");
        });

        progressBar.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(progressBar);

        panel.add(Box.createRigidArea(new Dimension(0, 5)));
        panel.add(updateLabel);

        panel.add(Box.createRigidArea(new Dimension(0, 5)));

        JButton closeButton = new JButton(PageManager.cancelText);
        closeButton.addActionListener(e -> {
            UpdaterManager.stopUpdate();
            StateBridge.setLoadingState(LoadingState.CONTINUE);
        });
        closeButton.setPreferredSize(new Dimension(60, 25));
        closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(closeButton);

        panel.add(Box.createRigidArea(new Dimension(0, 5)));
    }
}
